#ifndef __AEX_SGW_DEVICE_H__
#define __AEX_SGW_DEVICE_H__

// #include "sgwService.h"
#define SGW_TYPE_MIN            0x10
#define SGW_TYPE_BS				0x10 		//智能⽹关底板
#define SGW_TYPE_EC				0x11 		//边缘计算⽹关
#define SGW_TYPE_ACDC			0x12 		//AC-DC电源
#define SGW_TYPE_UPS			0x13 		//UPS电源
#define SGW_TYPE_AI				0x14 		//模拟输⼊AI
#define SGW_TYPE_AO				0x15 		//模拟输出AO
#define SGW_TYPE_DI				0x16 		//数字输⼊DI
#define SGW_TYPE_DO				0x17 		//数字输出DO
#define SGW_TYPE_NET2UART		0x18 		//串⼝模块
#define SGW_TYPE_NET2CAN		0x19 		//CAN模块
#define SGW_TYPE_NET2WIRELESS	0x1A 		//⽆线模块
#define SGW_TYPE_4G				0x1B 		//4G&WIFI模块
#define SGW_TYPE_NETSWITCH		0x1C 		//⽹络交换机模块
#define SGW_TYPE_PROFIBUS		0x1D 		//PROFIBUS PD模块
#define SGW_TYPE_AIO_BS			0x1E		//AIO底板模块
#define SGW_TYPE_MAX            0x1F    

#define SERVERDST_ADDR		0x00					//
#define GW_BROADCAST_ADDR	0x1F					//具有网关功能的模块需要处理的目标消息
#define SLOT_BROADCAST_ADDR	0xF0

#define BROADCAST_ADDR		0xFF					//所有模块需要处理的目标消息

#define get_slot_num(x)  ((x>>4)&0x0F)
#define get_slot_mod(x)  ((x&0x0F)|SGW_TYPE_MIN)
#define is_mod_broadcast(x)  ((x == BROADCAST_ADDR) || (get_slot_num(x) == SLOT_BROADCAST_ADDR) || (get_slot_mod(x)==GW_BROADCAST_ADDR))
#define is_gw_broadcast(x)  ((get_slot_mod(x)==GW_BROADCAST_ADDR))
#define is_slot_broadcast(x)  ((x&0xF0)==SLOT_BROADCAST_ADDR)

typedef struct sgw_device{
    unsigned char id;               //模块的ID
    unsigned char type;             //模块的类型
    unsigned char slot;             //模块的插槽号
    unsigned char modaddr;          //模块的总线地址，总线地址为槽位号<<8|模块类型
    unsigned char hasNet;           //模块是否有网络
    unsigned char bus;              //模块的总线是否在线
    unsigned char online;           //模块是否在线
    unsigned char error;            //模块是否有错误
    unsigned char uuid[33];         //模块的UUID
    struct sgw_device *next;
}SGWDEVICE,*PSGWDEVICE;

#define MAX_SGW_DEVICE_NUM      13

#define SGW_GETMODID()          (SGW_TYPE_EC)
void sgw_device_init();
PSGWDEVICE sgw_add_device();
void sgw_clear_devices();
int sgw_load_devices_from(char *fn);
int sgw_save_devices_tobuf(char *buf,int len);
int sgw_save_devices_tofile(char *fn);

unsigned char sgw_get_status();
void sgw_set_bus(unsigned char bus);
void sgw_set_online(unsigned char online);
void sgw_set_error(unsigned char error);
int sgw_device_has_net(unsigned char modaddr);


static inline char *get_mod_by_id(unsigned char mid)
{
    switch(mid){
    case SGW_TYPE_BS:	 		//智能⽹关底板
        return "sgw-bs";
    case  SGW_TYPE_EC:   		//边缘计算⽹关
        return "sgw-ec";
    case  SGW_TYPE_ACDC: 		//AC-DC电源
        return "sgw-acdc";
    case  SGW_TYPE_UPS: 		//UPS电源
        return "sgw-ups";
    case  SGW_TYPE_AI:   		//模拟输⼊AI
        return "sgw-ai";
    case  SGW_TYPE_AO:   		//模拟输出AO
        return "sgw-ao";
    case  SGW_TYPE_DI:   		//数字输⼊DI
        return "sgw-di";
    case  SGW_TYPE_DO:   		//数字输出DO
        return "sgw-do";
    case  SGW_TYPE_NET2UART:	//串⼝模块
        return "sgw-net2uart";
    case  SGW_TYPE_NET2CAN:		//CAN模块
        return "sgw-net2can";
    case  SGW_TYPE_NET2WIRELESS:	//⽆线模块
        return "sgw-net2wireless";
    case  SGW_TYPE_4G:   		//4G&WIFI模块
        return "sgw-4g";
    case  SGW_TYPE_NETSWITCH:	//⽹络交换机模块
        return "sgw-netswitch";
    case  SGW_TYPE_PROFIBUS:		//PROFIBUS PD模块
        return "sgw-profibus";
    case  SGW_TYPE_AIO_BS:		//AIO底板模块
        return "sgw-aio-bs";
    default:
        return "smartgw";
    }
}

#endif